package com.huzz.config;

import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.strategy.SaStrategy;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

/**
 * Sa-Token配置
 *
 * @author 童年的纸飞机
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    @Autowired
    public void rewriteSaStrategy() {
        // 重写 Token 生成策略
        SaStrategy.me.createToken = (loginId, loginType) -> {
            return SaFoxUtil.getRandomString(64);    // 随机60位长度字符串
        };
    }


    /**
     * 注册 Sa-Token 全局过滤器
     * @return
     */
    @Bean
    public SaServletFilter getSaServletFilter() {
        return new SaServletFilter()
                .addInclude("/**")
                .setAuth(obj -> {
                    // 校验 Same-Token 身份凭证
                    SaSameUtil.checkCurrentRequestToken();
                })
                .setExcludeList(excludePathPatterns)
                .setError(e -> SaResult.error(e.getMessage()));
    }

    private static List<String> excludePathPatterns = Arrays.asList(
            "/doc.html/**",
            "/swagger-ui.html/**",
            "/swagger-resources/**",
            "/webjars/**",
            "/v2/**",
            "/error",
            "/favicon.ico",
            "/csrf"
    );

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，打开注解式鉴权功能
        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
    }

//    /**
//     * 注册 Sa-Token 拦截器，打开注解式鉴权功能
//     */
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // 注册 Sa-Token 拦截器，打开注解式鉴权功能
//        registry.addInterceptor(new SaInterceptor(handler -> {
//                    SaRouter.match("/**", "/api/sa-token/login", r -> StpUtil.checkLogin());
//                    SaRouter.match("/admin/**", r -> StpUtil.checkRoleOr("admin", "super-admin"));
//                    SaRouter.match("/user/**", r -> StpUtil.checkPermission("user"));
//                    SaRouter.match("/admin/**", r -> StpUtil.checkPermission("admin"));
//                    SaRouter.match("/goods/**", r -> StpUtil.checkPermission("goods"));
//                    SaRouter.match("/orders/**", r -> StpUtil.checkPermission("orders"));
//                    SaRouter.match("/notice/**", r -> StpUtil.checkPermission("notice"));
//                    SaRouter.match("/comment/**", r -> StpUtil.checkPermission("comment"));
//
//                }))
//                .addPathPatterns("/**")
//                .excludePathPatterns(excludePathPatterns);
//    }
//
//    private static List<String> excludePathPatterns = Arrays.asList(
//            "/doc.html/**",
//            "/swagger-ui.html/**",
//            "/swagger-resources/**",
//            "/webjars/**",
//            "/v2/**",
//            "/error",
//            "/favicon.ico",
//            "/csrf"
//    );



}
